#include "addmqttdialog.h"
#include "ui_addmqttdialog.h"

AddMqttDialog::AddMqttDialog(QString title,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddMqttDialog)
{
    ui->setupUi(this);
    this->setWindowTitle("添加");
    if(!title.isEmpty())
        this->setWindowTitle(title);
    connect(ui->pushButton_ok,&QPushButton::clicked,this,&QDialog::accept);
    connect(ui->pushButton_cance,&QPushButton::clicked,this,&QDialog::reject);
}

AddMqttDialog::~AddMqttDialog()
{
    delete ui;
}

void AddMqttDialog::setData(MqttData data)
{
    ui->lineEdit->setText(data.topic);
    ui->radioButton_onoff->setChecked(data.type == MqttData::eSwitch);
    ui->radioButton_analog->setChecked(data.type == MqttData::eAnalog);
    ui->radioButton_text->setChecked(data.type == MqttData::eText);
    ui->spinBox->setValue(data.qos);
    ui->checkBox->setChecked(data.publish);
}

MqttData AddMqttDialog::getData()
{
    MqttData data;
    data.topic = ui->lineEdit->text();
    if(ui->radioButton_onoff->isChecked())
        data.type = MqttData::eSwitch;
    else if(ui->radioButton_analog->isChecked())
        data.type = MqttData::eAnalog;
    else if(ui->radioButton_text->isChecked())
        data.type = MqttData::eText;
    else
        data.type = MqttData::eText;
    data.qos = ui->spinBox->value();
    data.publish = ui->checkBox->isChecked();
    return data;
}

